package com.mma.assigment_registrationform;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterScreen extends Activity {

	private EditText username, password, email, city;
	private Button register;
	private DataManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_screen);
		manager = DataManager.getInstance();
		initLayout();

	}

	private void initLayout() {

		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		email = (EditText) findViewById(R.id.email);
		city = (EditText) findViewById(R.id.city);

		register = (Button) findViewById(R.id.registerButton);
		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String usernameValue = username.getText().toString();
				String passwordValue = password.getText().toString();
				String emailValue = email.getText().toString();
				if (usernameValue.length() == 0 || passwordValue.length() == 0
						|| emailValue.length() == 0) {
					Toast.makeText(getApplicationContext(),
							"All fields are requiard", Toast.LENGTH_SHORT)
							.show();
				} else {

					if (!manager.isUserExists(usernameValue)) {
						UserAccount account = new UserAccount(usernameValue,
								passwordValue, emailValue);
						String cityValue = city.getText().toString();
						account.setAddress(cityValue);
						
						manager.addUser(usernameValue, account);
						Toast.makeText(getApplicationContext(), "User created",
								Toast.LENGTH_SHORT);
						finish();
					} else {

						Toast.makeText(getApplicationContext(),
								"User with this name exists! ",
								Toast.LENGTH_LONG).show();
					}

				}

			}
		});

	}
}
